<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

define('IN_ADMIN_COMPONENT_CONTROLLER', 'true');

require_once('includes/components/CurrenciesComponent.php');

class CurrenciesAdministrationComponent extends CurrenciesComponent
{
	function CurrenciesAdministrationComponent()
	{
	}

	function action(&$params, $action)
	{
		if ($action == 'Update') 
			return $this->edit($params);
		else
			return $this->query($params);
	}

	function edit(&$params) 
	{
		$i = 0;
		while (true)
		{
			$i++;
			if ($i > 100) break;

			$currencyId = (int)$params['currencyId-'.$i];
			if ($currencyId == 0) break;

			$currencyName = prepare_input($params['currencyName-'.$i]);
			if ($currencyName == null || strlen($currencyName) == 0) continue;
			$currencyCode = prepare_input($params['currencyCode-'.$i]);
			if ($currencyCode == null || strlen($currencyCode) == 0) continue;
			$currencySymbolLeft = prepare_input($params['currencySymbolLeft-'.$i]);
			$currencySymbolRight = prepare_input($params['currencySymbolRight-'.$i]);
			$currencyDecimalPoint = prepare_input($params['currencyDecimalPoint-'.$i]);
			$currencyThousandsPoint = prepare_input($params['currencyThousandsPoint-'.$i]);
			$currencyDecimalPlaces = (int)$params['currencyDecimalPlaces-'.$i];
			
			$deleteRow = (boolean)$params['currencyDelete-'.$i];
					
			if ($deleteRow) {
				if ($currencyId < 1) continue;
				$sql = 'delete from ' . TABLE_CURRENCIES . ' where currencies_id=' . $currencyId;
				mysql_query($sql) or die(mysql_error());
			}
			else {
				$sql_data_array = array();
				$sqlAction = 'insert';
				$where = '';
				if ($currencyId > 0) {
					$sqlAction = 'update';
					$where = 'currencies_id=' . $currencyId;
				}
				else {
					$sql_data_arry['currencies_id']=$currencyId;
				}
				$sql_data_array['title'] = $currencyName;
				$sql_data_array['code'] = $currencyCode;
				$sql_data_array['symbol_left'] = $currencySymbolLeft;
				$sql_data_array['symbol_right'] = $currencySymbolRight;
				$sql_data_array['decimal_point'] = $currencyDecimalPoint;
				$sql_data_array['thousands_point'] = $currencyThousandsPoint;
				$sql_data_array['decimal_places'] = $currencyDecimalPlaces;
				db_perform(TABLE_CURRENCIES, $sql_data_array, $sqlAction, $where);

				if ($sqlAction == 'insert') {
					$currencyId = mysql_insert_id();
				}
	
				if ($currencyCode != DEFAULT_CURRENCY) {
					$rate = quote_oanda_currency($currencyCode, DEFAULT_CURRENCY);
					if (empty($rate)) $rate = quote_xe_currency($currencyCode, DEFAULT_CURRENCY);
					if (!empty($rate))
					{
						$sql = 'update ' . TABLE_CURRENCIES . ' set value = \'' . $rate . '\', last_updated = now() ' . 
						'where currencies_id = \'' . $currencyId . '\'';
						mysql_query($sql) or die(mysql_error());
					}
				}
			}
		}

		clean_cache();
		return $this->query($params);
	}
}
?>
